package com.watt.gateway.config;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class InterceptorConstant {
    public static Map<String, Boolean> path_map = new HashMap<String, Boolean>();
    @Value("${NO_Interceptor_Path}")
    private String paths;

    @Value("${NO_Interceptor_Service_Id}")
    private String serviceIds;

    private void initMap() {
        if (path_map.size() == 0) {
            if (StringUtils.isNotBlank(paths)) {
                String[] arrs = paths.split(",");
                for (String string : arrs) {
                    path_map.put(string, Boolean.TRUE);
                }
            }
        }
    }


    /**
     * 后缀地址
     * @param path
     * @return
     */
    public Boolean isPathInterceptor(String path) {
        this.initMap();
        Boolean isExc = path_map.get(path);
        return isExc == null ? false : isExc;
    }


    /**
     * 判断服务是否需要token
     * @param serviceId
     * @return
     */
    public boolean isNotInterceptorService(String serviceId) {
        if (StringUtils.contains(serviceIds,serviceId)){
            return true;
        }
        return false;
    }


    public boolean isStaticResource(String path) {
        if (path.indexOf("static") != -1) {
            return true;
        }
        return false;
    }
}
